package cn.doctor.assistant.pad.base;

import android.app.Activity;
import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;

import cn.doctor.assistant.pad.MyApplication;
import cn.doctor.assistant.pad.ui.UI;

/**
 * Created by jiaolf on 2016/2/22.
 * 网络错误的处理
 */
public class ErrorHandler {
    public static void dismissProgressDialog() {
        Activity activity = MyApplication.getInstance().getCurrentActivity();
        if ((activity instanceof BaseNetActivity)) {
            ((BaseNetActivity) activity).stopProgress();
        }
    }

    public static Response.ErrorListener getErrorListener() {
        return new Response.ErrorListener() {
            public void onErrorResponse(VolleyError volleyError) {
                ErrorHandler.dismissProgressDialog();
                ErrorHandler.onError(volleyError);
            }
        };
    }

    public static void onError(VolleyError paramVolleyError) {
        String msg = paramVolleyError.getMessage();
        if (msg == null) {
            msg = "error message is null";
        }
        Log.e("jzsn", msg);
        NetworkResponse localNetworkResponse = paramVolleyError.networkResponse;
        int statusCode = 0;
        if (localNetworkResponse != null) {
            statusCode = paramVolleyError.networkResponse.statusCode;
        }
        String str2 = null;
        if ((paramVolleyError instanceof NoConnectionError)) {
            str2 = "网络无连接";
        }else if ((paramVolleyError instanceof TimeoutError)) {
            str2 = "网络超时";
        } else if ((paramVolleyError instanceof ServerError)) {
            str2 = "服务器错误";
        } else if ((paramVolleyError instanceof ParseError)) {
            str2 = "解析错误";
        } else {
            str2 = "网络错误";
        }

        if (statusCode > 0) {
            str2 = statusCode + " " + str2;
        }

        UI.showToast(str2);
    }
}
